package com.coocaa.ops.api.v1.content.biz;

import com.coocaa.ops.api.common.validator.PolicyValidator;
import com.coocaa.ops.api.v1.content.mapper.IPluginMapper;
import com.coocaa.ops.api.v1.content.mapper.ITabInfoMapper;
import com.coocaa.ops.common.core.biz.tab.constant.TabTypeConstants;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author tangminghong
 * @date 2020-06-29
 */
@Service
public class TabInfoBizImpl implements ITabInfoBiz {

    @Autowired
    private ITabInfoMapper tabInfoMapper;
    @Autowired
    private IPluginMapper pluginMapper;

    @Override
    public TabInfoEntity getTabInfo(Integer tabId, Integer policyCategory) {
        TabInfoEntity tab;
        if (PolicyValidator.isFormalPolicy(policyCategory)) {
            tab = tabInfoMapper.queryTabInfo(tabId);
        } else {
            tab = tabInfoMapper.queryTabInfoForTestPolicy(tabId);
        }
        handlePluginInfo(tab);
        return tab;
    }

    private void handlePluginInfo(TabInfoEntity tab) {
        if (tab == null) {
            return;
        }
        if (tab.getPluginSelfId() != null && tab.getPluginSelfId() != 0 && TabTypeConstants.isPlugin(tab.getTabType())) {
            tab.setPluginInfo(pluginMapper.queryPluginInfo(tab.getPluginSelfId()));
        }
    }

}
